package com.mida.app.knw.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mida.app.knw.service.DetailCodingSystemService;
import com.mida.dms.mdm.api.provider.IDataSetApi;
import com.mida.dms.mdm.api.provider.IDetailCodingSystemApi;
import com.mida.dms.mdm.dto.DetailCodingSystemCitationDto;
import com.mida.dms.mdm.param.DataSetPageParam;
import com.mida.dms.mdm.pojo.auto.MsdDataSet;
import com.mida.dms.mdm.pojo.auto.MsdDetailCodingSystem;
import com.mida.dms.mdm.vo.DetailCodingSystemPageVo;
import com.mida.dms.mdm.vo.PullDownMenuVO;
import com.mida.tool.common.result.ListResult;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.mida.tool.common.result.BaseResult.buildListSuccess;

/**
 * @ClassName: DetailCodingSystemServiceImpl
 * @Description: 编码体系service
 * @Author: wang xiao le
 * @Date: 2021/6/21 17:45
 **/
@Service
public class DetailCodingSystemServiceImpl implements DetailCodingSystemService {
    @Autowired
    private IDetailCodingSystemApi iDetailCodingSystemApi;
    @Autowired
    private IDataSetApi iDataSetApi;

    /**
     * 获取值集map
     *
     * @param code      编码体系标志符
     * @param supersoid 上级值soid
     * @param isRoot    是否有根
     * @return Map  key为dataSetSoid value为dataSetName
     * @throws Exception
     */
    @Override
    public Map<Long, String> getDataSetMapByDetailCode(String code, Long supersoid, boolean isRoot) {
        ListResult<List<PullDownMenuVO>> drawer = iDetailCodingSystemApi.drawer(code, supersoid, isRoot,null);
        Map<Long, String> dataSetMap = new HashMap<>();
        if (ObjectUtils.isNotEmpty(drawer.getData())) {
            List<PullDownMenuVO> dataSetList = drawer.getData();
            //将值集soid为key,值集name为value放入map中
            for (PullDownMenuVO pullDownMenuVO : dataSetList) {
                if (dataSetMap.get(Long.valueOf(pullDownMenuVO.getCode())) == null) {
                    dataSetMap.put(Long.valueOf(pullDownMenuVO.getCode()), pullDownMenuVO.getValue());
                }
            }

        }
        return dataSetMap;
    }

    /**
     * 获取值集对象
     *
     * @param dataSetSoid 值集soid
     * @return 值集对象
     * @throws Exception
     */
    @Override
    public MsdDataSet getConceptSoidByDataSetSoid(Long dataSetSoid) {
        if (dataSetSoid == null) {
            return new MsdDataSet();
        }
        ListResult<MsdDataSet> msdDataSet = iDataSetApi.getConceptSoidByDataSetSoid(dataSetSoid);

        MsdDataSet dataSetTmp = new MsdDataSet();
        if (ObjectUtils.isNotEmpty(msdDataSet.getData())) {
            dataSetTmp = msdDataSet.getData();
        }

        return dataSetTmp;
    }

    /**
     * 获取值集对象List
     *
     * @param list 值集soid集合
     * @return 值集对象List
     */
    @Override
    public List<MsdDataSet> getConceptSoidsByDataSetSoids(List<Long> list) {

        List<MsdDataSet> listMsdDataSet = new ArrayList<MsdDataSet>();

        if (list == null || list.size() == 0) {
            return listMsdDataSet;
        }

        ListResult<List<MsdDataSet>> listMsdDataSetTmp =
                iDataSetApi.getConceptSoidsByDataSetSoids(list);

        if (ObjectUtils.isNotEmpty(listMsdDataSetTmp.getData())) {
            listMsdDataSet = listMsdDataSetTmp.getData();
        }

        return listMsdDataSet;
    }

    @Override
    public List<Long> getDataSetSoidsByConceptSoids(List<Long> list) {

        List<Long> listDataSetSoid = new ArrayList<Long>();

        if (list == null || list.size() == 0) {
            return listDataSetSoid;
        }

        ListResult<List<Long>> listTmp = iDataSetApi.getDataSetSoidsByConceptSoids(list);

        if (ObjectUtils.isNotEmpty(listTmp.getData())) {

            listDataSetSoid = listTmp.getData();
        }

        return listDataSetSoid;
    }


    /**
     * 根据值集soid获取值集name
     *
     * @param dataSetSoid 值集soid
     * @return 值集name
     */
    @Override
    public String getDataSetName(Long dataSetSoid) {
        String name = "";
        if (dataSetSoid == null) {
            return name;
        }
        ListResult<MsdDataSet> msdDataSet = iDataSetApi.getConceptSoidByDataSetSoid(dataSetSoid);

        MsdDataSet dataSetTmp = new MsdDataSet();
        if (ObjectUtils.isNotEmpty(msdDataSet.getData())) {
            dataSetTmp = msdDataSet.getData();
            name = dataSetTmp.getDataSetMeaning();
        }

        return name;
    }

    @Override
    public Long getDerailCodingSystemSoid(Long dataSetSoid) {
        Long derailCodingSystemSoid = null;
        if (dataSetSoid == null) {
            return derailCodingSystemSoid;
        }
        ListResult<MsdDataSet> msdDataSet = iDataSetApi.getConceptSoidByDataSetSoid(dataSetSoid);

        MsdDataSet dataSetTmp = new MsdDataSet();
        if (ObjectUtils.isNotEmpty(msdDataSet.getData())) {
            dataSetTmp = msdDataSet.getData();
            if (ObjectUtils.isNotEmpty(dataSetTmp)) {
                derailCodingSystemSoid = dataSetTmp.getDerailCodingSystemSoid();
            }
        }

        return derailCodingSystemSoid;
    }

    /**
     * 根据值集soidlist获取值集nameMap
     *
     * @param list 值集soid集合
     * @return Map  key为dataSetSoid value为dataSetName
     */
    @Override
    public Map<Long, String> getDataSetNameMap(List<Long> list) {
        List<MsdDataSet> listMsdDataSet = new ArrayList<MsdDataSet>();

        Map<Long, String> map = new HashMap<Long, String>();

        if (list == null || list.size() == 0) {
            return map;
        }

        ListResult<List<MsdDataSet>> listMsdDataSetTmp =
                iDataSetApi.getConceptSoidsByDataSetSoids(list);

        if (ObjectUtils.isNotEmpty(listMsdDataSetTmp.getData())) {
            listMsdDataSet = listMsdDataSetTmp.getData();

            for (MsdDataSet dataSet : listMsdDataSet) {
                if (map.get(dataSet.getDataSetSoid()) == null) {
                    map.put(dataSet.getDataSetSoid(), dataSet.getDataSetMeaning());
                }
            }
        }

        return map;
    }

    /**
     * 根据值集soidlist获取值集descriptionMap
     *
     * @param list 值集soid集合
     * @return Map  key为dataSetSoid value为dataSetDescription
     */
    @Override
    public Map<Long, String> getDataSetDescriptionMap(List<Long> list) {
        List<MsdDataSet> listMsdDataSet = new ArrayList<MsdDataSet>();

        Map<Long, String> map = new HashMap<Long, String>();

        if (list == null || list.size() == 0) {
            return map;
        }

        ListResult<List<MsdDataSet>> listMsdDataSetTmp =
                iDataSetApi.getConceptSoidsByDataSetSoids(list);

        if (ObjectUtils.isNotEmpty(listMsdDataSetTmp.getData())) {
            listMsdDataSet = listMsdDataSetTmp.getData();

            for (MsdDataSet dataSet : listMsdDataSet) {
                if (map.get(dataSet.getDataSetSoid()) == null) {
                    map.put(dataSet.getDataSetSoid(), dataSet.getDataSetDescription());
                }
            }
        }

        return map;
    }


    /**
     * 根据编码体系soidlist获取编码体系cnNameMap
     *
     * @param list 值集soid集合
     * @return Map  key:derailCodingSystemSoid value:cnName
     */
    @Override
    public Map<Long, String> getDetailCodingNameMap(List<Long> list) {
        List<MsdDetailCodingSystem> data = new ArrayList<>();
        Map<Long, String> map = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            return map;
        }
        ListResult<List<MsdDetailCodingSystem>> msdDetailCodingSystemList = iDetailCodingSystemApi.findListBySoids(list);
        if (ObjectUtils.isNotEmpty(msdDetailCodingSystemList)) {
            data = msdDetailCodingSystemList.getData();
            if (CollectionUtils.isNotEmpty(data)) {
                for (MsdDetailCodingSystem detailCodingSystem : data) {
                    if (map.get(detailCodingSystem.getDerailCodingSystemSoid()) == null) {
                        map.put(detailCodingSystem.getDerailCodingSystemSoid(), detailCodingSystem.getCnName());
                    }
                }
            }
        }
        return map;
    }

    /**
     * 根据编码体系soidlist获取编码体系beanMap
     *
     * @param list 值集soid集合
     * @return Map  key:derailCodingSystemSoid value:cnName
     */
    @Override
    public Map<Long, MsdDetailCodingSystem> getDetailCodingMap(List<Long> list) {
        List<MsdDetailCodingSystem> data = new ArrayList<>();
        Map<Long, MsdDetailCodingSystem> map = new HashMap<>();
        if (CollectionUtils.isEmpty(list)) {
            return map;
        }
        ListResult<List<MsdDetailCodingSystem>> msdDetailCodingSystemList = iDetailCodingSystemApi.findListBySoids(list);
        if (ObjectUtils.isNotEmpty(msdDetailCodingSystemList)) {
            data = msdDetailCodingSystemList.getData();
            if (CollectionUtils.isNotEmpty(data)) {
                for (MsdDetailCodingSystem detailCodingSystem : data) {
                    if (map.get(detailCodingSystem.getDerailCodingSystemSoid()) == null) {
                        map.put(detailCodingSystem.getDerailCodingSystemSoid(), detailCodingSystem);
                    }
                }
            }
        }
        return map;
    }

    /**
     * 根据编码体系soid获取编码体系cnName
     *
     * @param detailCodingSystemSoid 编码体系soid
     * @return cnName
     */
    @Override
    public String getDetailCodingName(Long detailCodingSystemSoid) {

        String cnName = "";
        if(ObjectUtils.isEmpty(detailCodingSystemSoid)){
            return cnName;
        }
        ListResult<MsdDetailCodingSystem> one = iDetailCodingSystemApi.findOneBySoid(detailCodingSystemSoid);
        if(ObjectUtils.isNotEmpty(one)){
            MsdDetailCodingSystem data = one.getData();
            if(ObjectUtils.isNotEmpty(data)){
                cnName = data.getCnName();
            }
        }

        return cnName;
    }

    /**
     * 1.根据编码体系标志符获取值集列表
     *
     * @param code
     * @return
     */
    @Override
    public ListResult<List<PullDownMenuVO>> drawer2(String code, Long supersoid, boolean isRoot)  throws Exception {
        ListResult<List<PullDownMenuVO>> drawer = iDetailCodingSystemApi.drawer(code, supersoid, isRoot,null);
        return drawer;
    }
    /**
     * 2.根据概念soid查询值集（概念中使用）
     *
     * @param dataSetPageParam 请求参数
     * @return
     */
    @Override
    public ListResult<Page<MsdDataSet>> findListByConceptSoid(DataSetPageParam dataSetPageParam) {

        ListResult<Page<MsdDataSet>> listByConceptSoid = iDataSetApi.findListByConceptSoid(dataSetPageParam);

        return listByConceptSoid;
    }
    /**
     * 3.查询所有编码体系（概念中使用）
     *
     * @param vo 请求参数
     * @return
     */
    @Override
    public ListResult<List<DetailCodingSystemCitationDto>> findAll(DetailCodingSystemPageVo vo) {
        ListResult<List<DetailCodingSystemCitationDto>> all = iDetailCodingSystemApi.findAll(vo);
        return iDetailCodingSystemApi.findAll(vo);
    }

    @Override
    public ListResult<Page<MsdDataSet>> findListByDerailCodingSystemSoid(DataSetPageParam param) {
        return iDataSetApi.findListByDerailCodingSystemSoid(param);

    }


    @Override
    public ListResult<List<MsdDataSet>> getDataSetList(Long derailCodingSystemSoid, String search) {
        ListResult result = new ListResult();
        DataSetPageParam dataSetPageParam = new DataSetPageParam();
        dataSetPageParam.setDerailCodingSystemSoid(derailCodingSystemSoid);
        ListResult<Page<MsdDataSet>> resultTmp = new ListResult<Page<MsdDataSet>>();
        resultTmp = iDataSetApi.findListByDerailCodingSystemSoid(dataSetPageParam);
        List<MsdDataSet> list = new ArrayList<>();
        Page<MsdDataSet> page = new Page<MsdDataSet>();
        if (resultTmp != null
                && resultTmp.getData() != null
                && page.getRecords() != null
                && page.getRecords().size() > 0) {
            page = resultTmp.getData();
            list = page.getRecords();
        }
        buildListSuccess(result, list);
        return result;
    }
}
